package controller;

import dao.PhoneDao;
import dao.impl.PhoneDaoImpl;
import entity.Phone;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;

/**
 * 从前端传回数据
 * 查找一个
 */
@WebServlet("/findOne.do")
public class FindPhoneById extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        /**
         * 接收前端传回的数据 “id”
         */
        String id = req.getParameter("id");
        PhoneDao phoneDao = new PhoneDaoImpl();
        try {
            //利用id查找到手机的数据并暂存
            Phone phone =  phoneDao.selectPhoneById(Integer.parseInt(id));

            req.setAttribute("phone",phone);
            req.getRequestDispatcher("/updatePhone.jsp").forward(req,resp);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }
}
